package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DzTixxMapper;
import com.ruoyi.system.domain.DzTixx;
import com.ruoyi.system.service.IDzTixxService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 题库选项Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-02
 */
@Service
public class DzTixxServiceImpl implements IDzTixxService 
{
    @Autowired
    private DzTixxMapper dzTixxMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询题库选项
     * 
     * @param xxId 题库选项主键
     * @return 题库选项
     */
    @Override
    public DzTixx selectDzTixxByXxId(Long xxId)
    {
        return dzTixxMapper.selectDzTixxByXxId(xxId);
    }

    /**
     * 查询题库选项列表
     * 
     * @param dzTixx 题库选项
     * @return 题库选项
     */
    @Override
    public List<DzTixx> selectDzTixxList(DzTixx dzTixx)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        dzTixx.setDatascope(datascope);
        if(datascope==4){
            dzTixx.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                dzTixx.setCheckHuiyuanId(huiyuan.getId());
            }else{
                dzTixx.setCheckHuiyuanId(0l);
            }

        }
        return dzTixxMapper.selectDzTixxList(dzTixx);
    }

    @Override
    public List<DzTixx> selectDzTixxListWeb(DzTixx dzTixx)
    {
        return dzTixxMapper.selectDzTixxList(dzTixx);
    }

    /**
     * 新增题库选项
     * 
     * @param dzTixx 题库选项
     * @return 结果
     */
    @Override
    public int insertDzTixx(DzTixx dzTixx)
    {
        return dzTixxMapper.insertDzTixx(dzTixx);
    }

    /**
     * 修改题库选项
     * 
     * @param dzTixx 题库选项
     * @return 结果
     */
    @Override
    public int updateDzTixx(DzTixx dzTixx)
    {
        return dzTixxMapper.updateDzTixx(dzTixx);
    }

    /**
     * 批量删除题库选项
     * 
     * @param xxIds 需要删除的题库选项主键
     * @return 结果
     */
    @Override
    public int deleteDzTixxByXxIds(Long[] xxIds)
    {
        return dzTixxMapper.deleteDzTixxByXxIds(xxIds);
    }

    /**
     * 删除题库选项信息
     * 
     * @param xxId 题库选项主键
     * @return 结果
     */
    @Override
    public int deleteDzTixxByXxId(Long xxId)
    {
        return dzTixxMapper.deleteDzTixxByXxId(xxId);
    }

    @Override
    public int deleteDzTixxByTiId(Long tiId) {
        return dzTixxMapper.deleteDzTixxByTiId(tiId);
    }
}
